﻿<%@ Control Language="C#" AutoEventWireup="true" CodeFile="UCItem.ascx.cs" Inherits="A_controls_UCItem" %>

<script type="text/javascript">
    var uc_fun_add, uc_fun_modified, uc_fun_canceld;
    var uc_fun_bat_add, uc_fun_bat_canceld;

    function BatchAddItem(categoryId, onadded, oncanceld) {
        if (!categoryId) return;
        uc_fun_bat_add = uc_fun_bat_canceld = null;

        if (typeof (onadded) == "function") uc_fun_bat_add = onadded;
        if (typeof (oncanceld) == "function") uc_fun_bat_canceld = oncanceld;

        $('#uc_item_bat_tbTitle').val('');
        $('#uc_item_bat_tbUrl').val('');

        $("#dialog_item_batchadd").dialog({
            width: 780,
            height: 400,
            modal: true,
            resizable: false,
            title: "批量添加条文",
            zIndex: 101,
            close: function() {
                if (uc_fun_bat_canceld) {
                    uc_fun_bat_canceld.call(this);
                }
                uc_fun_bat_add = uc_fun_bat_canceld = null;
            }
        });
        //set Category
        var slt = $('#uc_item_bat_sltCategory').empty().cascadeselect('clear', false);
        uc_init_category(categoryId, '#uc_item_bat_sltCategory', '#dialog_item_batchadd');
    }

    function uc_item_bat_add() {
        var cid = $('#uc_item_bat_sltCategory').cascadeselect('getvalue');
        if (!cid) {
            alert('请选择目录。');
            return;
        }
        var titleStr = $('#uc_item_bat_tbTitle').val();
        var urlStr = $('#uc_item_bat_tbUrl').val();

        if (titleStr.length < 1) {
            alert('标题不能为空。');
            return;
        }

        $.ajax({
            url: "ajax.aspx?action=addbatitem&cid=" + cid,
            async: false,
            dataType: "json",
            data: { 'title': titleStr, 'url': urlStr },
            type: 'POST',
            beforeSend: function() {
                $('#dialog_item_batchadd').masklayer('show', { zIndex: 102, Text: '正在保存，请稍候...' });
            },
            success: function(d) {
                d = eval(d);
                if (!d.Error) {
                    if (uc_fun_bat_add) {
                        uc_fun_bat_add.call(this, cid);
                    }
                    uc_fun_bat_add = uc_fun_bat_canceld = null;
                    $("#dialog_item_batchadd").dialog("close");
                } else {
                    alert('保存失败！错误信息：' + d.Error);
                }
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                alert('发送请求失败！错误信息：' + errorThrown);
            },
            complete: function() {
                $('#dialog_item_batchadd').masklayer('hide');
            }
        });
    }

    function uc_item_bat_cancel() {
        if (uc_fun_bat_canceld) {
            uc_fun_bat_canceld.call(this);
        }
        uc_fun_bat_add = uc_fun_bat_canceld = null;
        $("#dialog_item_batchadd").dialog("close");
    }

    function uc_init_buttons() {
        $('#uc_modifyArea').hide();
        $('#uc_addArea').hide();
        $('#uc_chkArea').hide();
    }


    function DeleteItems(arr, ondeleted, onerror) {
        if (!arr || arr.length < 1) return;
        var idStr = arr.join(',');
        $.ajax({ url: "ajax.aspx?action=deleteitems&itemids=" + idStr,
            async: false,
            dataType: "json",
            type: 'GET',
            success: function(d) {
                d = eval(d);
                if (d.Result) {
                    if (ondeleted)
                        ondeleted.call(this, d.Result);
                } else if (onerror) {
                    onerror.call(this, d.Error);
                }
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                if (onerror)
                    onerror.call(this, errorThrown);
            }
        });
    }

    function CheckItems(arr, onchcked, onerror) {
        if (!arr || arr.length < 1) return;
        var idStr = arr.join(',');
        $.ajax({ url: "ajax.aspx?action=checkitems&itemids=" + idStr,
            async: false,
            dataType: "json",
            type: 'GET',
            success: function(d) {
                d = eval(d);
                if (d.Result) {
                    if (onchcked)
                        onchcked.call(this, d.Result);
                } else if (onerror) {
                    onerror.call(this, d.Error);
                }
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                if (onerror)
                    onerror.call(this, errorThrown);
            }
        });
    }

    function AddItem(categoryId, onadded, oncanceld) {
        if (!categoryId) return;
        uc_fun_add = uc_fun_canceld = null;

        if (typeof (onadded) == "function") uc_fun_add = onadded;
        if (typeof (oncanceld) == "function") uc_fun_canceld = oncanceld;

        //set value;
        $('#tbId').val('');
        $('#uc_item_tbTitle').val('');
        $('#uc_item_tbDescription').val('');
        $('#uc_item_tbLinkUrl').val('');
        $('#uc_item_tbLogo').val('');
        $('#uc_item_tbTopState').val('');

        uc_init_buttons();
        $('#uc_addArea').show();
        $("#dialog_item").dialog({
            width: 450,
            height: 360,
            modal: true,
            resizable: false,
            title: "添加条文",
            zIndex: 101,
            close: function() {
                if (uc_fun_canceld) {
                    uc_fun_canceld.call(this);
                }
                uc_fun_modified = null;
                uc_fun_canceld = null;
            }
        });

        //set Category
        var slt = $('#uc_item_sltCategory').empty().cascadeselect('clear', false);
        uc_init_category(categoryId, '#uc_item_sltCategory', '#dialog_item');
    }

    function ModifyItem(data, onmodified, oncanceld) {
        if (!data) return;
        uc_fun_modified = uc_fun_canceld = null;

        if (typeof (onmodified) == "function") uc_fun_modified = onmodified;
        if (typeof (oncanceld) == "function") uc_fun_canceld = oncanceld;

        //set value;
        $('#tbId').val(data.ItemId);
        $('#uc_item_tbTitle').val(data.Title);
        $('#uc_item_tbDescription').val(data.Description);
        $('#uc_item_tbLinkUrl').val(data.LinkUrl);
        $('#uc_item_tbLogo').val(data.LogoPath);
        $('#uc_item_tbTopState').val(data.TopState);

        uc_init_buttons();
        if (!!data.IsChecked) {
            $('#uc_modifyArea').show();
        } else {
            $('#uc_chkArea').show();
        }
        $("#dialog_item").dialog({
            width: 450,
            height: 360,
            modal: true,
            resizable: false,
            title: "编辑条文",
            zIndex: 101,
            close: function() {
                if (uc_fun_canceld) {
                    uc_fun_canceld.call(this);
                }
                uc_fun_modified = null;
                uc_fun_canceld = null;
            }
        });

        //set Category
        var slt = $('#uc_item_sltCategory').empty().cascadeselect('clear', false);
        uc_init_category(data.CategoryId, '#uc_item_sltCategory', '#dialog_item');
    }

    function uc_init_category(categoryId, slt, container, callback) {
        $.ajax({
            url: "ajax.aspx?action=getrecursiveparents&cid=" + categoryId,
            //async: false,
            dataType: "json",
            beforeSend: function() {
                $(container).masklayer('show', { zIndex: 102, Text: '正在获取目录...' });
            },
            success: function(data) {
                if (data.Result) {
                    data = eval(data.Result);
                    var hash = [];
                    while (data.length > 0) {
                        hash.push((data.shift()).CategoryId);
                    }
                    $.ajax({ url: "ajax.aspx?action=getcategories",
                        async: false,
                        dataType: "json",
                        success: function(data) {
                            if (data.Result) {
                                data = eval(data.Result);
                                if (hash) {
                                    var sid = hash.shift();

                                    var i = 0, len = data.length, str = '';
                                    while (i < len) {
                                        str += "<option value='" + data[i].CategoryId + "'";
                                        if (data[i].CategoryId == sid) {
                                            str += " selected='selected'";
                                        }
                                        str += ">" + data[i].CategoryName + "</option>";
                                        i++;
                                    }
                                    $(slt).append(str);
                                    if (!sid) {
                                        $(slt).prepend("<option value='@' selected='selected'>根目录</option>");
                                        hash = null;
                                    }
                                    $(slt).removeAttr('disabled');
                                    $(slt).cascadeselect({
                                        url: 'ajax.aspx?action=getcategories',
                                        key: 'pid',
                                        type: "GET",
                                        valuedatafield: 'CategoryId',
                                        textdatafield: 'CategoryName',
                                        selecteddatafield: 'S', //'Selected',
                                        unselectedoption: { value: '@', text: '请选择目录' },
                                        beforebinding: function() {
                                            $(container).masklayer('show', { zIndex: 102, Text: '正在获取目录...' });
                                        },
                                        ondatabinding: function(select, data) {
                                            if (data.Result) {
                                                data = data.Result;
                                                if (data.length == 0) {
                                                    $(select).remove();
                                                    hash = null;
                                                    return false;
                                                }
                                                if (hash) {
                                                    var sid = hash.shift();
                                                    if (!!sid) {
                                                        var i = 0, len = data.length;
                                                        while (i < len) {
                                                            if (data[i].CategoryId == sid) {
                                                                data[i].S = true;
                                                            }
                                                            i++;
                                                        }
                                                        if (i == len) {
                                                            hash = null;
                                                        }
                                                    } else {
                                                        hash = null;
                                                    }
                                                }
                                                return data;
                                            } else {
                                                return false;
                                            }
                                        },
                                        oncompleted: function() {
                                            if (!hash) {
                                                $(container).masklayer('hide');
                                                if (callback && typeof callback == "function")
                                                    callback.call(this);
                                            }
                                        }
                                    });
                                }
                            }
                        }
                    });
                }
            },
            complete: function() {
                $(container).masklayer('hide');
            }
        });
    }

    function uc_item_add() {
        var data = {}, invalidsucceed = true, msg = '';

        data.Title = $('#uc_item_tbTitle').val();
        data.CategoryId = $('#uc_item_sltCategory').cascadeselect('getvalue');
        data.Description = $('#uc_item_tbDescription').val();
        data.LinkUrl = $('#uc_item_tbLinkUrl').val();
        data.LogoPath = $('#uc_item_tbLogo').val();
        data.TopState = $('#uc_item_tbTopState').val();
        data.IsChecked = true;

        if (invalidsucceed && $.trim(data.Title).length == 0) {
            msg = '标题不能为空！';
            invalidsucceed = false;
        }
        if (invalidsucceed && isNaN(data.CategoryId)) {
            msg = '请选择目录！';
            invalidsucceed = false;
        }
        if (!invalidsucceed) {
            alert(msg);
            return;
        }

        $.ajax({
            url: "ajax.aspx?action=additem",
            async: false,
            dataType: "json",
            data: data,
            type: 'POST',
            beforeSend: function() {
                $('#dialog_item').masklayer('show', { zIndex: 102, Text: '正在保存，请稍候...' });
            },
            success: function(d) {
                d = eval(d);
                if (!d.Error) {
                    if (uc_fun_add) {
                        uc_fun_add.call(this, data);
                    }
                    uc_fun_add = null;
                    uc_fun_canceld = null;
                    $("#dialog_item").dialog("close");
                } else {
                    alert('保存失败！错误信息：' + d.Error);
                }
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                alert('发送请求失败！错误信息：' + errorThrown);
            },
            complete: function() {
                $('#dialog_item').masklayer('hide');
            }
        });
    }

    function uc_item_save() {
        var data = {}, invalidsucceed = true, msg = '';

        data.ItemId = $('#tbId').val();
        data.Title = $('#uc_item_tbTitle').val();
        data.CategoryId = $('#uc_item_sltCategory').cascadeselect('getvalue');
        data.Description = $('#uc_item_tbDescription').val();
        data.LinkUrl = $('#uc_item_tbLinkUrl').val();
        data.LogoPath = $('#uc_item_tbLogo').val();
        data.TopState = $('#uc_item_tbTopState').val();
        data.IsChecked = true;

        if (!data.ItemId) {
            msg = 'ItemID为空！';
            invalidsucceed = false;
        }
        if (invalidsucceed && $.trim(data.Title).length == 0) {
            msg = '标题不能为空！';
            invalidsucceed = false;
        }
        if (invalidsucceed && isNaN(data.CategoryId)) {
            msg = '请选择目录！';
            invalidsucceed = false;
        }
        if (!invalidsucceed) {
            alert(msg);
            return;
        }
        var str = decodeURIComponent(data);

        $.ajax({
            url: "ajax.aspx?action=saveitem",
            async: false,
            dataType: "json",
            data: data,
            type: 'POST',
            beforeSend: function() {
                $('#dialog_item').masklayer('show', { zIndex: 102, Text: '正在保存，请稍候...' });
            },
            success: function(d) {
                d = eval(d);
                if (!d.Error) {
                    if (uc_fun_modified) {
                        uc_fun_modified.call(this, true, (!!d.Result ? eval(d.Result) : data));
                    }
                    uc_fun_modified = null;
                    uc_fun_canceld = null;
                    $("#dialog_item").dialog("close");
                } else {
                    alert('保存失败！错误信息：' + d.Error);
                }
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                alert('发送请求失败！错误信息：' + errorThrown);
            },
            complete: function() {
                $('#dialog_item').masklayer('hide');
            }
        });
    }

    function uc_item_cancel() {
        if (uc_fun_canceld) {
            uc_fun_canceld.call(this);
        }
        uc_fun_modified = null;
        uc_fun_canceld = null;
        $("#dialog_item").dialog("close");
    }
</script>

<div id="dialog_item">
    <input type="hidden" id="tbId" />
    <table>
        <tr>
            <td>
                <label>
                    条文标题</label>
            </td>
            <td>
                <input type="text" id="uc_item_tbTitle" style="width: 332px;" />
            </td>
        </tr>
        <tr>
            <td>
                <label>
                    目录</label>
            </td>
            <td>
                <select id="uc_item_sltCategory">
                </select>
            </td>
        </tr>
        <tr>
            <td>
                <label>
                    条文描述</label>
            </td>
            <td>
                <textarea id="uc_item_tbDescription" cols="50" rows="4"></textarea>
            </td>
        </tr>
        <tr>
            <td>
                <label>
                    链接地址</label>
            </td>
            <td>
                <input type="text" id="uc_item_tbLinkUrl" style="width: 332px;" />
            </td>
        </tr>
        <tr>
            <td>
                <label>
                    Logo</label>
            </td>
            <td>
                <input type="text" id='uc_item_tbLogo' style="width: 332px;" />
            </td>
        </tr>
        <tr>
            <td>
                <label>
                    优先级</label>
            </td>
            <td>
                <input type="text" id="uc_item_tbTopState" style="width: 30px;" />
            </td>
        </tr>
        <tr>
            <td colspan="2" style="text-align: right;">
                <div id="uc_modifyArea" style="display: none;">
                    <button type="button" onclick="uc_item_save();">
                        保存修改</button>
                    <button type="button" onclick="uc_item_cancel();">
                        取 消</button>
                </div>
                <div id="uc_chkArea" style="display: none;">
                    <button type="button" onclick="uc_item_save();">
                        保存修改并通过审核</button>
                    <button type="button" onclick="uc_item_cancel();">
                        取 消</button>
                </div>
                <div id="uc_addArea" style="display: none;">
                    <button type="button" onclick="uc_item_add();">
                        添加条文</button>
                    <button type="button" onclick="uc_item_cancel();">
                        取 消</button>
                </div>
            </td>
        </tr>
    </table>
</div>
<div id="dialog_item_batchadd">
    <table>
        <tr>
            <td colspan="2">
                <select id="uc_item_bat_sltCategory">
                </select>
            </td>
        </tr>
        <tr>
            <td>
                <fieldset>
                    <legend>标题</legend>
                    <textarea id="uc_item_bat_tbTitle" cols="50" rows="10"></textarea>
                </fieldset>
            </td>
            <td>
                <fieldset>
                    <legend>条文链接</legend>
                    <textarea id="uc_item_bat_tbUrl" cols="50" rows="10"></textarea>
                </fieldset>
            </td>
        </tr>
        <tr>
            <td colspan="2" style="text-align: right;">
                <button onclick="uc_item_bat_add();">
                    添 加</button>
                <button onclick="uc_item_bat_cancel();">
                    取 消</button>
            </td>
        </tr>
    </table>
</div>
